package com.xuan.temper.controller;

import com.xuan.temper.dao.DateDao;
import com.xuan.temper.entity.ExponentialTemperature;
import com.xuan.temper.entity.ResponeBean;
import com.xuan.temper.service.ExponentialTemperatureService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * (ExponentialTemperature)表控制层
 *
 * @author makejava
 * @since 2020-08-31 15:41:05
 */
@RestController
@RequestMapping("exponentialTemperature")
public class ExponentialTemperatureController {
    /**
     * 服务对象
     */
    @Resource
    private ExponentialTemperatureService exponentialTemperatureService;

    @Autowired
    private DateDao dateDao;

    /**
     * 通过主键查询单条数据
     *
     * @param
     * @return 单条数据
     */
    @RequestMapping(value = "/todatTemp", method = RequestMethod.POST)
    public ResponeBean selectOne(@Param("stockCode") String[] stockCode, @Param("todayTime") String todayTime) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        ArrayList<ExponentialTemperature> arrayList = new ArrayList<>();
        com.xuan.temper.entity.Date load = dateDao.load(format.format(format.parse(todayTime)));
        Boolean holiday = load.getHoliday();
        int i = 1;
        if (holiday) {
            while (true) {
                Date date = new Date();//获取当前时间
                Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
                calendar.add(Calendar.DAY_OF_MONTH, -i); //当前时间减去一天，即一天前的时间
                Date resDate = calendar.getTime();
                String s = format.format(resDate);
                com.xuan.temper.entity.Date load1 = dateDao.load(s);
                if (!load1.getHoliday()) {
                    String day = load1.getDate();
                    for (String s1 : stockCode) {
                        ExponentialTemperature temperature = exponentialTemperatureService.load(s1, day);
                        arrayList.add(temperature);
                    }
                    break;
                }
                i++;
            }

        } else {
            for (String s : stockCode) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format1 = sdf.format(sdf.parse(todayTime));
                //当前时间是否小于5点
                String hour = format1.substring(11,13);
                int sss = Integer.parseInt(hour);
                if ( sss<16) {

                    Calendar calendar = Calendar.getInstance();
                    Date date = null;
                    date = sdf.parse(todayTime);
                    calendar.setTime(date);
                    int day = calendar.get(Calendar.DATE);
                    calendar.set(Calendar.DATE, day - 1);
                    String lastDay = format.format(calendar.getTime());
                    com.xuan.temper.entity.Date loadDay = dateDao.load(lastDay);
                    Boolean isHoliday = loadDay.getHoliday();
                    int aaa = 1;
                    if (isHoliday) {
                        while (true) {
                            //Date date = new Date();//获取当前时间
                            Calendar calendar1 = Calendar.getInstance(); //创建Calendar 的实例
                            calendar1.add(Calendar.DAY_OF_MONTH, -aaa); //当前时间减去一天，即一天前的时间
                            Date resDate = calendar1.getTime();
                            String s1 = format.format(resDate);
                            com.xuan.temper.entity.Date load1 = dateDao.load(s1);
                            if (!load1.getHoliday()) {
                                String day1 = load1.getDate();
                                    ExponentialTemperature temperature = exponentialTemperatureService.load(s, day1);
                                    arrayList.add(temperature);
                                break;
                            }
                            aaa++;
                        }
                    } else {
                        ExponentialTemperature temperature = exponentialTemperatureService.load(s, lastDay);
                        arrayList.add(temperature);
                    }
                } else {
                    ExponentialTemperature temperature = exponentialTemperatureService.load(s, format.format(format.parse(todayTime)));
                    arrayList.add(temperature);
                }
            }
        }
        return new ResponeBean("200", "success", arrayList);
    }

}
